import type { FormConfig } from '@/types/dynamic-form';

export const createUserFormConfig = (isEdit: boolean = false): FormConfig => ({
  key: 'user-form',
  title: isEdit ? '编辑用户' : '新增用户',
  description: isEdit ? '修改用户信息' : '添加新的用户',
  layout: 'vertical',
  labelAlign: 'left',
  requiredMark: true,
  initialValues: isEdit ? {} : {
    status: true,
  },
  sections: [
    {
      key: 'basic',
      title: '基本信息',
      fields: [
        ...(isEdit ? [{
          key: 'id',
          label: 'ID',
          type: 'input',
          hidden: true,
        }] : []),
        {
          key: 'username',
          label: '用户名',
          type: 'input',
          placeholder: '请输入用户名',
          required: true,
          disabled: isEdit, // 编辑时禁用用户名
          rules: [
            { required: true, message: '请输入用户名' },
            { min: 3, max: 20, message: '用户名长度在3-20个字符之间' },
            { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线' },
          ],
        },
        {
          key: 'name',
          label: '姓名',
          type: 'input',
          placeholder: '请输入姓名',
          required: true,
          rules: [
            { required: true, message: '请输入姓名' },
            { max: 20, message: '姓名最多20个字符' },
          ],
        },
        {
          key: 'password',
          label: '密码',
          type: 'input',
          placeholder: '请输入密码',
          required: !isEdit, // 编辑时密码非必填
          hidden: isEdit, // 编辑时隐藏密码字段
          rules: [
            { required: !isEdit, message: '请输入密码' },
            { min: 6, max: 20, message: '密码长度在6-20个字符之间' },
          ],
          fieldProps: {
            type: 'password',
          },
        },
        {
          key: 'confirmPassword',
          label: '确认密码',
          type: 'input',
          placeholder: '请再次输入密码',
          required: !isEdit,
          hidden: isEdit,
          dependencies: ['password'],
          rules: [
            { required: !isEdit, message: '请确认密码' },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('password') === value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('两次输入的密码不一致'));
              },
            }),
          ],
          fieldProps: {
            type: 'password',
          },
        },
      ],
      col: 2,
      gutter: 16,
    },
    {
      key: 'contact',
      title: '联系方式',
      fields: [
        {
          key: 'mobile',
          label: '手机号',
          type: 'input',
          placeholder: '请输入手机号',
          required: true,
          rules: [
            { required: true, message: '请输入手机号' },
            { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式' },
          ],
        },
        {
          key: 'email',
          label: '邮箱',
          type: 'input',
          placeholder: '请输入邮箱',
          rules: [
            { type: 'email', message: '请输入正确的邮箱格式' },
          ],
        },
              ],
      col: 2,
      gutter: 16,
    },
      ],
  submitButton: {
    text: '确定',
    type: 'primary',
  },
  cancelButton: {
    text: '取消',
    type: 'default',
  },
});